{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "haar 特征像素 运算结果\n",
    "根据特征区分目标 阈值判决\n",
    "得到判决 机器学习"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "import cv2\r\n",
    "import numpy as np\r\n",
    "eye_xml = cv2.CascadeClassifier('haarcascade_eye.xml')\r\n",
    "face_xml = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')\r\n",
    "img = cv2.imread('3.jpg')\r\n",
    "\r\n",
    "gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\r\n",
    "#detect 1 data 2 缩放系数 3 目标大小\r\n",
    "faces = face_xml.detectMultiScale(gray,1.3,10,cv2.CASCADE_SCALE_IMAGE,(50,50),(100,100))\r\n",
    "print('face=',len(faces))\r\n",
    "#draw\r\n",
    "for (x,y,w,h) in faces:\r\n",
    "    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)\r\n",
    "    roi_face = gray[y:y+h,x:x+w]\r\n",
    "    roi_color = img[y:y+h,x:x+w]\r\n",
    "    eyes = eye_xml.detectMultiScale(roi_face)\r\n",
    "    print('eye=',len(eyes))\r\n",
    "    for (e_x,e_y,e_w,e_h) in eyes:\r\n",
    "        cv2.rectangle(roi_color,(e_x,e_y),(e_x+e_w,e_y+e_h),(0,255,0),2)\r\n",
    "cv2.imshow('src',img)\r\n",
    "cv2.waitKey(0)\r\n"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "face= 3\n",
      "eye= 2\n",
      "eye= 2\n",
      "eye= 3\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.8 64-bit ('base': conda)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "interpreter": {
   "hash": "73e03da126b73bfff3642ec5261d56fa25c444ea595de51041687efaa60dda41"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}